package matrix.world.volleyball;

import java.util.Random;
import sounds.MyMedia;
import ellements.Ball;
import ellements.Crab;
import ellements.CrabEnemy;
import ellements.drawable.Desk;
import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback  {
	
	public static final int LEVEL=1;
	
	private final int Playing = 0;
	public static final int GameOver = 2;
	public static final int FINAL = 3;
	
	public static int SCORE=0;
	
	public static int Width,Height;
	public static float rate;
	
	private Ball[] balls = new Ball[1]; 
	
	private Crab crab = null;
	private CrabEnemy crabEnemy = null;
	private Desk desk;
	
	private MyThread mThread;

	public static int Status;
	
	public MySurfaceView(Context context, int Width, int Height) {
		super(context);
//		setKeepScreenOn(true);
		getHolder().addCallback(this);
		MySurfaceView.Width = Width;
		MySurfaceView.Height = Height;
		rate = MySurfaceView.Height/320f;
		balls[0] = new Ball(getResources());
		crab = new Crab(getResources());
		crabEnemy = new CrabEnemy(getResources());
		desk = new Desk(getResources());
		balls[0].desk = desk;
		mThread = new MyThread(this);
	}

	private long t;
	public void doAnimate() {
		synchronized (balls) {
			switch (Status) {
			case Playing:
				Ball bal = balls[0];
				bal.animate();
				bal.collisionCrab(crab);
				bal.collisionCrab(crabEnemy);
				crab.animation();
				crabEnemy.animation(balls[0]);
				break;
			}
		}
		if (t<0){
			Random rd = new Random();
			t = rd.nextInt(500)+500;
			MyMedia.playMain();
		}else{
			t--;
		}
		
		
	}
	
	public void doDraw(Canvas c) {
		desk.draw(c);
		balls[0].doDraw(c);
		crabEnemy.draw(c);
		crab.draw(c);
		desk.drawFront(c);
	}

	// @Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {

	}

	// @Override
	public void surfaceCreated(SurfaceHolder holder) {
		if (!mThread.isAlive()) {
			mThread = new MyThread(this);
			mThread.setRunning(true);
			mThread.start();
			SCORE=0;
			Status=0;
		}
	}

	// @Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if (mThread.isAlive()) {
			mThread.setRunning(false);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e) {
		float x,x1=-1;
		if (e.getPointerCount()>1){
			x=e.getX(0);
			x1=e.getX(1);
		}else{
			x = e.getX();
		}
		if (Status==GameOver){
			switch (e.getAction()) {
			case MotionEvent.ACTION_DOWN:
				float y = e.getY();
				if (x>MySurfaceView.Width*0.5f-100*MySurfaceView.rate 
						&& x< MySurfaceView.Width*0.5f+100*MySurfaceView.rate 
						&& y>MySurfaceView.Height*0.4f-50*MySurfaceView.rate
						&& y<MySurfaceView.Height*0.4f+50*MySurfaceView.rate){
					playAgain();
				}
			}
		}else{
			synchronized (balls) {
				if (x<MySurfaceView.Width/2){
//					if (e.getAction() == MotionEvent.ACTION_MOVE){
						crab.setMoveTo(x);
//					}
					if (x1>MySurfaceView.Width/2){
						crab.jump();
					}
					return true;
				}else{
//					if (e.getAction() == MotionEvent.ACTION_DOWN){
						crab.jump();
//					}
				}
				
			}
		}
		return false;
	}

	private void playAgain(){
		Random rd = new Random();
		Status = Playing;
		SCORE = 0;
		balls[0].x = MySurfaceView.Width/2+rd.nextInt(MySurfaceView.Width/2);
		balls[0].y = 0;
		Ball.ballLiveNumber = 10;
		desk.updateSceneClear();
		desk.updateScene();
	}
	
}
